package com.test.followme;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.test.utilities.Utility;

import android.content.Context;
import android.util.Log;

public class MsgPrsFactory
{	
	JSONObject jsonObj = null;
	MsgPrsBase msgParser = null;

	public static final int msgHTTP = 0;
	public static final int msgGCM = 1;
	private int msgType;
	Context context;
	
	class RspData
	{
		public RspData()
		{
			retDesc = "";
			retCode = 0;
			userId = "";
			userName = "";
			ssnId = "";			
		}
		
		private int retCode;
		private String retDesc;
		private String userId;
		private String userName;
		private String ssnId;
	}
	
	RspData response;

	void initialize()
	{
		this.msgType = msgHTTP;
		response = new RspData();
	}
	
	void setJsonObj(String msg)
	{
		try
		{
			jsonObj = new JSONObject(msg);
		}
		catch (JSONException e)
		{
			Log.i("MsgPrsFactory", e.getMessage());
			e.printStackTrace();
		}
	}
	
	public MsgPrsFactory(Context context)
	{
		initialize();
		setJsonObj(null);
	}
	
	public MsgPrsFactory(Context context, String msg)
	{
		initialize();
		this.context = context;
		setJsonObj(msg);
	}
	
	public MsgPrsFactory(Context context, JSONObject json)
	{
		initialize();
		this.context = context;
		jsonObj = json;
	}
		
	public int getMsgType()
	{
		return msgType;
	}

	public void setMsgType(int msgType)
	{
		this.msgType = msgType;
	}

	public int getRetCode()
	{
		return response.retCode;
	}

	public void setRetCode(int retCode)
	{
		response.retCode = retCode;
	}
	
	public String getRetDesc()
	{
		return response.retDesc;
	}

	public void setRetDesc(String retDesc)
	{
		response.retDesc = retDesc;
	}
	
	public String getUserId()
	{
		return response.userId;
	}

	public void setUserId(String userId)
	{
		response.userId = userId;
	}
	
	public String getUserName()
	{
		return response.userName;
	}

	public void setUserName(String userName)
	{
		response.userName = userName;
	}
	
	public String getSsnId()
	{
		return response.ssnId;
	}

	public void setSsnId(String ssnId)
	{
		response.ssnId = ssnId;
	}

	public boolean parseMsg()
	{
		if (!(prsRetCode() && response.retCode == Globals.retCodeSuccess))
		{
			prsRetDesc();	
			return false;
		}
		
		if (!prsUserInfo())
		{
			return false;
		}
		
		
		if (!prsBlocks())
		{
			return false;
		}
		
		return true;
	}
	
	@SuppressWarnings("finally")
	private boolean prsRetCode()
	{
		if (msgType == msgGCM)
		{
			response.retCode = Globals.retCodeSuccess;
			return true;
		}
		
		try
		{
			response.retCode = jsonObj.getInt(Globals.keyRETCODE);
			Log.i("MsgPrsFactory", "retCode : " + response.retCode);
		}
		catch (JSONException e)
		{
			response.retCode = 0;
			e.printStackTrace();
		}
		finally
		{
			return response.retCode != 0;
		}
	}
	
	@SuppressWarnings("finally")
	private boolean prsRetDesc()
	{
		try
		{
			response.retDesc = jsonObj.getString(Globals.keyRETDESC);
		}
		catch (JSONException e)
		{
			response.retDesc = "";
			e.printStackTrace();
		}
		finally
		{
			return !Utility.isNullOrEmpty(response.retDesc);
		}
	}
	
	@SuppressWarnings("finally")
	private boolean prsUserInfo()
	{
		try
		{
			response.userId = jsonObj.getString(Globals.keyUSERID);
			response.userName = jsonObj.getString(Globals.keyUSERNAME);
			response.ssnId = jsonObj.getString(Globals.keySESSIONID);
		}
		catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}
		
		return true;
	}	
	
	@SuppressWarnings("finally")
	private boolean prsBlocks()
	{
		try
		{
			JSONArray blocks = jsonObj.getJSONArray(Globals.keyBLOCKS);
			if (blocks.length() == 0)
			{
				return false;
			}
			
			for (int i = 0; i < blocks.length(); ++i)
			{
				JSONObject block = blocks.getJSONObject(i);
				int type = block.getInt(Globals.keyTYPE);
				
				switch (type)
				{
					case Globals.TYPE_NOTIFICATION:
						msgParser = new MsgPrsNtf(context, block);
						break;
					case Globals.TYPE_FOLLOW_REQUEST:
						msgParser = new MsgFlwReq(context, block);
						break;
					case Globals.TYPE_WHO_I_FOLLOW:
						msgParser = new MsgWhoIFlw(context, block);
						break;
					case Globals.TYPE_WHO_FOLLOW_ME:
						msgParser = new MsgWhoFlwMe(context, block);
						break;
					default:
						return false;
				}
				
				if (!msgParser.parse())
				{
					Log.i("MsgPrsFactory", "msgParser.parse() returns false");
					return false;
				}
			}
		}
		catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}
		
		Log.i("MsgPrsFactory", "prsBlocks() end normally");
		return true;
	}	
}
